using System;
using System.Timers;
using EstimateTracker.ViewModels;
using JetBrains.Annotations;

namespace EstimateTracker
{
    public class StartToggleTimer : IToggleTimer
    {
        private readonly Timer _t;
        private readonly Action<double> _notifyTimerStoped;

        private readonly IToggleTimer _stopToggleTimer;

        public StartToggleTimer([NotNull] Timer t, [NotNull] Action<double> notify,[NotNull] Action<double> notifyTimerStoped )
        {
            if (t == null) throw new ArgumentNullException("t");
            if (notify == null) throw new ArgumentNullException("notify");
            if (notifyTimerStoped == null) throw new ArgumentNullException("notifyTimerStoped");

            _t = t;
            _notifyTimerStoped = notifyTimerStoped;
            _t.Elapsed += OnElapsed;
            _stopToggleTimer = new StopToggleTimer(_t, notify, _notifyTimerStoped);
        }

        private void OnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            Time = Time + 1;
            Notify(Time);
        }

        public IToggleTimer Toggle()
        {
            _t.Start();
            return _stopToggleTimer;
        }

        public double Time { get; private set; }
        public void Notify(double t)
        {
            _stopToggleTimer.Notify(Time);
        }

        public string Label { get { return "Stop"; } }
    }
}